< previous page page_316 next page >

Page 316
Tutorial 4
How DLL Calls Work:
Inside a Stack Frame
7017a6ead0e3c4111b47a554df321e9f.gif 7017a6ead0e3c4111b47a554df321e9f.gif
Runtime Error 49
Bad DLL Calling Convention Error.
Look familiar?
This is the error message you get when your Declare statement for a DLL or API call is incorrectif you're lucky. If you're not lucky, your declaration will have one of the many errors that Visual Basic can't detect. But that's another story.
So where does this error come from? Why does Visual Basic detect some DLL declaration errors and not others? To understand the answer, you'll have to look deep under the hood at the actual mechanism by which functions are calledand at something called a stack frame.
Stack frame? Sounds like one of those esoteric computer science terms that are designed to intimidate the uninitiated. That's probably because it is an esoteric computer science term designed to intimidate the uninitiated.
Seriouslyto understand a stack frame, you must first know what a stack is.
The Stack
A stack is a data structure in memory on which two standard operations are performed. These operations are "push," which places data on the stack, and "pop," which removes data from the stack. A stack is a ''Last In First Out" structure, in which the last data item placed on the stack is always the first one removed.
Figure T4-1 illustrates the push and pop operations on a simple stack that holds 32-bit Long values.
The first illustration shows the empty stack. Each stack has a stack pointer that points to the top value on the stack. This pointer can be a variable that contains a memory address, or (if the stack is implemented in an array) it can be the array index of the top data item in the array. It is not uncommon for stacks to be implemented using arrays in Visual Basic.
When a data item is pushed onto the stack, the stack pointer is moved to the next location, and the data is placed in the location referred to by the stack pointer. When the pop operation is executed, the data item referred to by the

 
< previous page page_316 next page >